package com.fenghongzhang.ivideo.ui

import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.widget.Button
import androidx.activity.ComponentActivity
import androidx.core.animation.doOnEnd
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.alibaba.android.arouter.launcher.ARouter
import com.fenghongzhang.ivideo.R
import com.fenghongzhang.ivideo.utils.LocaleUtils
import com.fenghongzhang.lib_common.ARouterCons


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        val splashScreen = installSplashScreen()
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

        //  切换语言
        findViewById<Button>(R.id.chagne_lan).setOnClickListener {
            LocaleUtils.setLocale(this, "en")
            recreate() // 重新创建 Activity
        }


        // 保持 SplashScreen 直到数据加载完成
//        splashScreen.setKeepOnScreenCondition {
//            println("setKeepOnScreenCondition")
        //返回ture 会一直卡着. false才能用
        //可以这里判断数据是否完成, 不建议用, 不能一直卡.
//            true
//        }
        //定义动画退出
        splashScreen.setOnExitAnimationListener { splashScreenView ->
            // 创建自定义动画
            val fadeOut = ObjectAnimator.ofFloat(
                splashScreenView.iconView,
                View.ALPHA,
                1f,
                0f
            )
            fadeOut.duration = 1000L
            fadeOut.interpolator = AccelerateInterpolator()
            fadeOut.doOnEnd { splashScreenView.remove() }

            // 开始动画
            fadeOut.start()
        }


    }

    override fun onResume() {
        super.onResume()
        ARouter.getInstance().build(ARouterCons.LOGIN_ACTIVITY).navigation()
    }
}